package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bigchun.seeland.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import bean.ApplicationDataBean;
import myapp.MyApp;
import myview.HorizontalListView;
import util.MyLog;
import util.OrderEventMessage;

/**
 * Created by fei on 2017/12/30.
 */

public class ApplicationRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_BANNER = 0;//头部轮播图
    public static final int TYPE_CLASSIFICATION = 1;//分类
    public static final int TYPE_SPECIAL = 2;//通知和专题页
    public static final int TYPE_ADVERT = 3;//广告
    public static final int TYPE_PREFERRED = 4;//为你优选
    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<ApplicationDataBean.DataBean> mDatas;
    private Context context;
    private MyApp myApp;
    private int width;
    private String type;


    public ApplicationRecyclerViewAdapter(List<ApplicationDataBean.DataBean> mDatas, Context context, MyApp myApp, int width,String type) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
        this.width = width;
        this.type = type;

    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }






    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<ApplicationDataBean.DataBean> datas) {
        this.mDatas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {

        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_application, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
        final ApplicationDataBean.DataBean bean = mDatas.get(pos);
        if (viewHolder instanceof Holder) {

            /*int twoimgwidth = width-DensityUtil.dip2px(context,48);
            int oneimgwidth = twoimgwidth/2;
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) ((Holder) viewHolder).img.getLayoutParams();
            layoutParams.width = oneimgwidth;
            ((Holder) viewHolder).img.setLayoutParams(layoutParams);*/

            ((Holder) viewHolder).nameTv.setText(mDatas.get(pos).getContent());
            ((Holder) viewHolder).contentTv.setText(mDatas.get(pos).getContent());
            HorAppImageAdapter imgadapter = new HorAppImageAdapter(context,mDatas.get(pos).getImages());
            ((Holder) viewHolder).horlistview.setAdapter(imgadapter);
            imgadapter.setData(mDatas.get(pos).getImages());
            ((Holder) viewHolder).horlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    MyLog.e("下标00000000000000",i+"_"+pos);
                    if ("应用搜索".equals(type)){
                        EventBus.getDefault().post(new OrderEventMessage("应用场景搜索",i+"_"+pos));

                    }else{
                        EventBus.getDefault().post(new OrderEventMessage("应用场景",i+"_"+pos));
                    }
                }
            });
            if (mListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(pos);
                    }
                });
            }
        }
    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private TextView nameTv;//名字
        private TextView contentTv;//内容
        private HorizontalListView horlistview;//内容
        private HorAppImageAdapter imgadapter;//内容

        public Holder(View itemView) {
            super(itemView);
            nameTv = (TextView) itemView.findViewById(R.id.tv_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            horlistview = itemView.findViewById(R.id.horlistview);


        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}